package algorithm;

/**
 * Two points search
 */
public class TwoPointsSearch {
    public static void main(String[] args) {
        TwoPointsSearch search = new TwoPointsSearch();
        int[] arr = new int[]{1, 2, 4, 5, 7, 8, 10, 14, 16, 17, 18, 24};
        System.out.println(search.search(0, 11, arr, 24));
    }

    public int search(int start, int end, int[] arrays, int mark) {
        if (end - start == 1) {
            return arrays[end] == mark ? end : arrays[start] == mark ? start : -1;
        }
        int t = (end + start) / 2;
        if (arrays[t] == mark) {
            return t;
        } else if (arrays[t] < mark) {
            return search(t, end, arrays, mark);
        } else {
            return search(start, t, arrays, mark);
        }
    }
}
